<?
include_once "fbmain.php";
include_once "configuration.php";
include_once "common_functions.php";
$put_scr=true;

// the following permits not to show the output
ob_start();
system("/bin/date");
echo("<html><head></head><body>");


//load the client PHP library of face.com
require_once("FaceRestClient.php");
//initialize API object with API key and Secret
$api = new FaceRestClient('b4f307030a9b1c76a16855fe283bf079', 'ecf806ce0037a68ad828f699e11aaf12');

// get values from previous steps
$filepath = $_GET['file'];
$dir_check = $_GET['dir_check'];
$dir_tmp = $_GET['dir_tmp'];
$dir_final = $_GET['dir_final'];
$dir_video = ($_GET['dir_video']);
$prefix_dir = $_GET['prefix_dir'];
$dir_final_log = $_GET['dir_final_log'];


//echo($dir_check);
//echo($filepath);
$number_faces_checked = 0;
//load faces saved in a file
$photos_saved_in_file = load_tabbed_file($filepath);
//print_r($photos_saved_in_file);
//echo(count($photos_saved_in_file));
//print_r($photos_saved_in_file[1][1]);
for ($fo=0;$fo<count($photos_saved_in_file);$fo++){
	$photos_for_recognizer[$fo] = $photos_saved_in_file[$fo][1];
	$photos_for_recognizer_x[$fo] = $photos_saved_in_file[$fo][2];
	$photos_for_recognizer_y[$fo] = $photos_saved_in_file[$fo][3];
	$counter[$fo] = $photos_saved_in_file[$fo][0];
}

//print_r($photos_for_recognizer);



$starttime_find_faces = compute_time();


for($opp=0;$opp<count($photos_for_recognizer);$opp++){

	$number_faces_checked = $number_faces_checked + count($photos_for_recognizer);
	
	$result = $api->faces_detect($photos_for_recognizer[$opp], null, null, null, 'normal');
	//print_r($result->{'photos'}[0]->{'tags'}[0]->{'recognizable'});

	//print_r($result);
	//echo(" ");
	//echo(" ");
	//$width = $result->{'photos'}[0]->{'width'};
	//echo($width);
	//$height = $result->{'photos'}[0]->{'height'};
	$tags = $result->{'photos'}[0]->{'tags'};
	$width = $result->{'photos'}[0]->{'width'};
	$height = $result->{'photos'}[0]->{'height'};
	if ($put_scr==true) {
	  echo("<br>");
	  echo("<br>I am processing the photo: ".$photos_for_recognizer[$opp]);
	  echo("<br>which is the same as the downloaded one: ".$counter[$opp]);
	  echo("<br>Number of faces inside the image: ".count($tags));
	}

	unset($face_center);

	$immm = @imagecreatefromjpeg($counter[$opp]);
	$immmSize[0]= imagesx($immm);
	$immmSize[1]= imagesy($immm);


	$col_ellipse3 = imagecolorallocate($immm, 0, 0, 255);
	//echo($opp);
	//echo(count($tags));
	//if($result->{'photos'}[0]->{'tags'}[0]->{'recognizable'}==1){
	if(count($tags)>0){
	//echo("<br> FB center x ".$photos_for_recognizer_x[$opp]." and y ".$photos_for_recognizer_y[$opp]);
//echo("ccc");
	foreach($tags as $key=>$value) {
	    if ($put_scr==true) echo("<br>".$key);
	    $face_center[$key]['x'] = $value->{'center'}->{'x'};
	    $face_center[$key]['y'] = $value->{'center'}->{'y'};
	    //echo("<br>face center x ".$face_center[$key]['x']." and y ".$face_center[$key]['y']);
	    if ($do_check_images==true){ imagefilledellipse($immm, $face_center[$key]['x'] *$immmSize[0]/100, $face_center[$key]['y']*$immmSize[1]/100, 12, 12, $col_ellipse3);}
	    if (isset($value->{'eye_left'}->{'y'})){	    
	      $face_eye_left[$key]['x']= $value->{'eye_left'}->{'x'};
	      $face_eye_left[$key]['y']= $value->{'eye_left'}->{'y'};
	    }
	    if ($do_check_images==true){ imagefilledellipse($immm, $face_eye_left[$key]['x'] *$immmSize[0]/100, $face_eye_left[$key]['y']*$immmSize[1]/100, 12, 12, $col_ellipse3);}
	    if (isset($value->{'eye_right'}->{'y'})){
	      $face_eye_right[$key]['x']= $value->{'eye_right'}->{'x'};
	      $face_eye_right[$key]['y']= $value->{'eye_right'}->{'y'};
	    }
	    if ($do_check_images==true){ imagefilledellipse($immm, $face_eye_right[$key]['x'] *$immmSize[0]/100, $face_eye_right[$key]['y']*$immmSize[1]/100, 12, 12, $col_ellipse3);}
	    $face_width[$key]['width']= $value->{'width'};
	    $face_height[$key]['height']= $value->{'height'};
	    $face_pitch[$key]['pitch']= $value->{'pitch'};
	    $face_roll[$key]['roll']= $value->{'roll'};
	    $face_yaw[$key]['yaw']= $value->{'yaw'};    
	    //echo("<br>".$face_width[$key]['width']."<fdfdsbr>");
	    //echo("<br>".$face_center[$key]['y']."<\br>");
	    //}
	}

	if ($do_check_images==true){
	$numpad=number_pad($opp,4);
	imagejpeg($immm,$dir_check."tmp_to_check_".$numpad.".jpg");

	}



	//
	// find the face which is closest to the fb tag:
	//
	$d = 1000;
	$D = 1000;

	$x0=$photos_for_recognizer_x[$opp];
	$y0=$photos_for_recognizer_y[$opp];
	if ($put_scr==true) echo("<br>\tI am checking ".count($face_center)." number of faces");
	//print_r(count($face_center));
	for($kk=0;$kk<count($face_center);$kk++){
		$x = $face_center[$kk]['x'];
		$y = $face_center[$kk]['y'];
		//echo("<br>x ".$x." and y ".$y);
		//echo("<br>x0".$x0." and y0".$y0);
	
		$d = sqrt(($x-$x0)*($x-$x0) + ($y-$y0)*($y-$y0));
		//if ($put_scr==true) echo("<br>all value of d.... ".$d);
		//echo("<br>".$d);
	
		if($d<$D) {
			$D=$d;
			$selected_face[$opp][0]=$kk;
			$selected_face[$opp][1]=$d;
			//echo("here");
			if ($put_scr==true) echo("<br>d value is ".$d);
		}
	
	}
	if ($put_scr==true) echo("<br>The face selected is the number ".($selected_face[$opp][0])." ");



	//
	// select all the data of the selected face
	//
	$new_face_present[$opp] = $tags;
	$new_face[$opp][0] = $counter[$opp]; // filename of the saved image
	$size_ph[$opp] = getimagesize($new_face[$opp][0]);
	//print_r(getimagesize($new_face[$opp][0])); 
	//echo("<br> isez".$size_ph[$opp][0]);
	$new_face[$opp][1] = $face_center[$selected_face[$opp][0]]['x']; //center x
	$new_face[$opp][2] = $face_center[$selected_face[$opp][0]]['y']; // center y
	if (isset($value->{'eye_left'}->{'y'})){
	  $new_face[$opp][3] = $face_eye_left[$selected_face[$opp][0]]['x']; //center x
	  $new_face[$opp][4] = $face_eye_left[$selected_face[$opp][0]]['y']; // center y
	  $new_face[$opp][5] = $face_eye_right[$selected_face[$opp][0]]['x']; //center x
	  $new_face[$opp][6] = $face_eye_right[$selected_face[$opp][0]]['y']; // center y
	}
	else{
	  $new_face[$opp][3] = 1; //center x
	  $new_face[$opp][4] = 1; // center y
	  $new_face[$opp][5] = 1; //center x
	  $new_face[$opp][6] = 1; // center y
	}
	$new_face[$opp][7] = $face_roll[$selected_face[$opp][0]]['roll'];  //rotation
	$new_face[$opp][8] = $face_width[$selected_face[$opp][0]]['width']*$face_height[$selected_face[$opp][0]]['height']; // area in percentage
	// full area is 100 * 100 = 10000.
	$new_face[$opp][9] = $face_width[$selected_face[$opp][0]]['width']*$size_ph[$opp][0]/100;
	//echo("<br> pixel face width :".$new_face[$opp][5]);
	$new_face[$opp][10] = $face_height[$selected_face[$opp][0]]['height']*$size_ph[$opp][1]/100;
	$new_face[$opp][11] = $face_center[$selected_face[$opp][0]]['x']*$size_ph[$opp][0]/100; //center x
	$new_face[$opp][12] = $face_center[$selected_face[$opp][0]]['y']*$size_ph[$opp][1]/100; // center y

	//echo("<br> pixel face heigth:".$new_face[$opp][6]);

	if ($selected_face[$opp][1]>50) $new_face_present[$opp]=0;
    }

}	

$endtime_find_faces = compute_time();
$totaltime_find_faces = ($endtime_find_faces - $starttime_find_faces);

$number_photo[3] = 0;

if ($put_scr==true) echo("<br>");
if ($put_scr==true) echo("<br>");

// write to file the found faces
$myFile_faces = $dir_check."faces_details.txt";
$fid = fopen($myFile_faces, 'w') or die("can't open file");
$json = json_encode($new_face);
fwrite($fid, $json);
fclose($fid);

// write to file the found faces
$myFile_faces_present = $dir_check."faces_present.txt";
$fid = fopen($myFile_faces_present, 'w') or die("can't open file");
$json = json_encode($new_face_present, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);
fwrite($fid, $json);
fclose($fid);

// write time information to file
$file_info_time = $dir_final_log."step2-find_faces.txt";
$fid_time = fopen($file_info_time, 'w') or die("can't open file");
fwrite($fid_time, $starttime_find_faces."\t".$endtime_find_faces."\t".$totaltime_find_faces."\t");
fclose($fid_time);


//write any output screen to a file to check 
echo("</body></html>");
$c=ob_get_contents();
ob_end_clean();
$fp=fopen($dir_final_log."02_find_faces.html", "wb");
fwrite($fp, $c);
fclose($fp);

// send data back for next steps
echo("file_faces=".$myFile_faces."&file_faces_present=".$myFile_faces_present."&dir_check=".$dir_check."&dir_tmp=".$dir_tmp."&prefix_dir=".$prefix_dir."&dir_final=".$dir_final."&file=".$filepath."&dir_video=".$dir_video."&dir_final_log=".$dir_final_log);





?>
